package com.woniuxy.lab.woniu_lab_boss.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniuxy.lab.woniu_lab_boss.common.Result;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 17292
 */
public class WebUtil {
	
	public static HttpServletRequest getRequest() {
		ServletRequestAttributes sra = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
		return sra.getRequest();
	}
	
	public static HttpServletResponse getResponse() {
		ServletRequestAttributes sra = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
		return sra.getResponse();
	}

	public static void json(int code,String message,HttpServletResponse response) throws IOException {

		Result result = Result.success(code,message,null);
		response.setCharacterEncoding("utf8");
		response.setContentType("application/json;charset=utf8");
		ObjectMapper om = new ObjectMapper();
		String json = om.writeValueAsString(result);
		response.getWriter().write(json);
		response.getWriter().flush();
	}
}





